import { Component, Input, ChangeDetectionStrategy, ChangeDetectorRef } from "@angular/core";
import { BaseCriteriaTemplateComponent } from "../baseType.component";
import { ReportingService } from "../../../../../reporting.service";
import { Subscription } from "rxjs";
import { ICriteriaTemplate, TemplateType } from "../../models/criteriaTemplate.interface";
import { ICriteriaOutput } from "../../models/save/criteriaOutput.interface";
import { MultiselectOutput } from "../../models/save/criteriaOutput.model";

@Component({
    selector: 'type7',
    templateUrl: 'type7.component.html',
    styleUrls: ['../../templateCriteria.component.scss', '../../../criteria.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush,
    providers: [{ provide: BaseCriteriaTemplateComponent, useExisting: Type7Component }]
})
export class Type7Component extends BaseCriteriaTemplateComponent implements ICriteriaTemplate {
    private loadSub: Subscription;
    public search: string;
    public items: any[] = [
        { id: '0', name: 'Compliant' },
        { id: '1', name: 'Watchlist' },
        { id: '2', name: 'Non-compliant' }
    ];
    public selected: any[] = [];
    public type: TemplateType = TemplateType.Multiselect;

    constructor(
        private toolsService: ReportingService,
        private cdRef: ChangeDetectorRef) {
        super(toolsService, cdRef);
    }

    @Input() level3: string;

    ngOnInit() {
        if (this.edit == 'true') {
            this.loadMultiSelectData('typeSevenSelectedItems');
            this.loadSub = this.load.subscribe(data => {
                this.selected = this.items.filter(x => data.indexOf(x.id) > -1) || [];
                this.cdRef.markForCheck();
            });
        }
    }

    ngOnDestroy() {
        if (this.loadSub)
            this.loadSub.unsubscribe();
    }

    public select(item: any) {
        if (this.selected.indexOf(item) == -1)
        {
            this.selected.push(item);
            BaseCriteriaTemplateComponent.changed.emit(true);
        }
    }

    public unselect(index: number) {
        this.selected.splice(index, 1);
        BaseCriteriaTemplateComponent.changed.emit(this.selected.length > 0);
    }

    public onEnter(value: string) {
        this.search = value;
    }

    public selectedDesc() {
        return this.selected.map(x => x.name).join(' and ');
    }

    public isEnabled(element: string): boolean {
        let ret: boolean = false;
        switch(element){
            case '0':
                ret = true;
                break;
            case '1':
                ret = this.hasPermission("api/v1/filters/{filterId}/criteria", "POST", [{ key: 'shortName', value: 'gccsnc' }]);
                break;
            case '2':
                ret = this.hasPermission("api/v1/filters/{filterId}/criteria", "POST", [{ key: 'shortName', value: 'gccswl' }]);
                break;
        }
        return ret;       
    }

    public save(): ICriteriaOutput {
        return new MultiselectOutput(
            this.level3,
            this.templateCriteria.criteriaLevel2,
            this.templateCriteria.id,
            this.templateCriteria.name,
            this.selected.map(function (o) { return o.id }));
    }
}